var aa = ajaxStart();
var timer = null;

function shopcar_edit() {
    var f = $('updateform');
    if(f.emptycar.value == 1 &&!confirm('您确认不需要这些物品了吗？')) {
        f.emptycar.value = 0;
        return false;
    }
    $('updatemsg').hide(); $('loading').show();
    try {
        aa.call('/shopcar.php', 'ajaxShopcarUpdate', shopcaredit_result, f.serialize());
        f.disable();
        return false;
    }
    catch(e) {
        return true;
    }
}

function shopcaredit_result(result) {
    var i;
    var d = $('updatemsg');
    var f = $('updateform');
    var id;
    $('loading').hide();
    try {
        var stat = result.result[0].data;
        if(stat == 1 || stat == 8) {
            i = '更新成功';
            if(stat == 8)i += '。某些物品数量超过库存，我们自动为您调整了数量。<span style="cursor:hand" onclick="window.open(\'/faq/24\', \'faq\')"><u>详情<\/u><\/span>';
            if(result.price == 'undefined' || result.price == null) return _emptycar();
            var dib = new Array();
            for(var j = 0; j < result.price.length; j++) {
                id = result.price[j].id[0].data;
                if(Prototype.Browser.IE)
                    $('price' + id).innerText = result.price[j].price[0].data;
                else
                    $('price' + id).textContent = result.price[j].price[0].data;
                $('amount' + id).value = result.price[j].amount[0].data;
                if(result.price[j].amount[0].data == 0)dib.push(id);
            }
            if(Prototype.Browser.IE) {
                $('totalprice').innerText = result.total[0].data;
                $('caramount').innerHTML = $('totalamount').innerText = result.totalamount[0].data;
            }
            else {
                $('totalprice').textContent = result.total[0].data;
                $('caramount').innerHTML = $('totalamount').textContent = result.totalamount[0].data;
            }
            if(result.totalamount[0].data == 'undefined' || result.totalamount[0].data == null || result.totalamount[0].data == '')return _emptycar();
        }
        if(stat == 2) i = '<font color=FFFF66>更新失败<\/font>，请检查您的输入后再提交';
    }
    catch(e) {
        if(f.emptycar.value == 1) {
            return _emptycar();
            location.reload();
            $('updateform').hide();
            i = '加载中';
            var emptycar = 1;
        }
        else {
            i = '服务器错误，请稍候再试';
            d.style.backgroundColor = '#FF6600';
        }
    }
    if(emptycar == 1) {
        location.href = "/shopcar/checkout";
    }
    else {
        try {
            $('updateform').enable();
            dib.each(function db(id) {
                    $('amount' + id).disabled = true}
                    );
            if(i) d.innerHTML = '&nbsp;&nbsp;&nbsp;' + i + '……';
            $('updatemsg').show();
            new Effect.Round('div', 'updatemsg');
            location.href = "#top";
            if(timer)window.clearTimeout(timer);
            timer = setTimeout("$('updatemsg').style.display='none';", 10000);
        }
        catch(e) {
        }
    }
}

function _emptycar() {
	try {
		$('promo').hide();
		$('cartext').replace('您的购物车中<br />目前是空空的哦');
		$('icon_shopcar').removeClassName('icon_shopcar_full');
		$('icon_shopcar').addClassName('icon_shopcar_empty');
		$('shopcar').replace('<div class="errbox roundNormal">&nbsp;&nbsp;&nbsp;您的购物车中没有任何物品，为何不看看我们的 <a href="/whathot" class="white">热门物品</a>?</div>');
		$('shopcarlist_msg').hide();
	}
	catch(e) {}
    bodyOnLoad();
}

function checkout_submit() {
    var f = $('checkoutform');
    try {
        aa.call('/checkout.php', 'ajaxCheckout', checkout_result, $('checkoutform').serialize());
	    $('checkoutform').disable();
        return false;
    }
    catch(e) {
        return true;
    }
}

function checkout_result(result) {
    var i;
    var d = $('updatemsg');
    try {
        var stat = result.result[0].data;
        if(stat != 1 && stat != 2 && stat != 4 && stat != 8) {
            location.reload();
            return false;
        }
        if(stat == 1) {
            i = '订单已提交成功，请稍候';
            location.href = '/pay.php/' + result.indentid[0].data + '/1';
            return false;
        }
        if(stat == 2)i = '<font color=FFFF66>更新失败<\/font>，请检查您的输入后再提交';
        if(stat == 4) {
            i = '为保证物品能及时快捷的到达您的收中，请填写完整您的收货信息，谢谢!';
            d.style.backgroundColor = '#FF6600';
            d.style.color = '#FFF';
        }
        if(stat == 8) {
            alert('注意：由于某些物品库存不够，我们调整了您的订单。请再次确认！');
            location.reload();
        }
    }
    catch(e) {
        i = '服务器错误，请稍候再试';
        d.style.backgroundColor = '#FF99FF';
    }
    $('checkoutform').enable();
    if(i) {
        d.innerHTML = '&nbsp;&nbsp;&nbsp;' + i + '……';
        $('updatemsg_div').show();
        new Effect.Round('div', 'updatemsg');
        location.href = "#top";
        if(timer)window.clearTimeout(timer);
        timer = setTimeout("$('updatemsg_div').style.display='none';", 20000);
    }
}

function change_voucher() {
    try {
        $('voucher_btn_sub').disabled = true;
        aa.call('/voucher.php', 'ajaxGetVoucher', change_voucher_result, 'str=' + $F('voucher_code') + '&price=' + totalprice);
    }
    catch(e) {
        alert('服务器错误，请稍候再试');
    }
    return false;
}

function change_voucher_result(result) {
    $('voucher_btn_sub').disabled = false;
    var voucher_price = parseFloat(result.price[0].data);
    var error = parseInt(result.error[0].data);
    var errmsg = '代码无效&nbsp;&nbsp;';
    if(error > 1) {
        if(error == 4) errmsg = '您的购物金额至少要为 &yen;' + voucher_price;
        voucher_price = 0;
        $('voucher_btn', 'voucher_area').invoke('toggle');
    }
    $('voucher_price').innerHTML = voucher_price ? '&yen; - ' + voucher_price + '&nbsp;&nbsp;':errmsg;
    if(voucher_price) $('totalprice').innerHTML = Math.round((totalprice-voucher_price)*100)/100;
    $('voucher_code_h').value = $F('voucher_code');
    change_sumprice();
}

function change_sumprice(postprice) {
    if(!postprice) {
        var radioGrp = $('checkoutform').postfee;
        for(var i = 0; i < radioGrp.length; i++) {
            if(radioGrp[i].checked) var postprice = parseInt(radioGrp[i].value);
        }
    }
    var sumprice = Math.round((parseFloat($('totalprice').innerHTML)+postprice)*100)/100;
    $('sumprice').innerHTML = (parseInt(sumprice) > 0 ? sumprice : 0);
}

function animatePoof(imageToPoof) {
    poofCount++;
    if(poofCount >= 6) {
        document.images[imageToPoof].src = "/images/blank.gif";
        clearInterval(poofTimer);
    } else {
        document.images[imageToPoof].src = eval("poof" + poofCount + ".src");
    }
}

function addtocar(id, msg) {
    if(msg == null) {
        msg = true;
        showmsg = msg;
    }
    $('tips','updatemsg').invoke('hide'); $('loading').show();
    aa.call('/addtocar.php', 'ajaxAddToCar', addtocar_result, 'id=' + id);
    return false;
}

function alertme(id) {
    window.open('/alertme.php/' + id + '/pop/', '_alertme', 'width=300,height=150');
    return true;
}

function addtocar_result(result) {
    var i;
    var d = $('updatemsg');
    $('loading').hide();
    try {
        var stat = result.result[0].data;
        var c = result.count[0].data;
        if(stat == 1)i = '成功了！您目前共有 ' + c + ' 件战利品。点击左面的按钮可以快速结帐';
        if(stat == 2)i = '失败了，没有找到您要加入的物品';
        if(stat == 8)i = '乐铺提醒您：您购物车中的数量已超过该物品库存，您不能够购买更多。<a title="通过我们的帮助系统了解详情" href="/faq.php/23" target="_blank" class="white">详情<\/a>';
        if(stat == 4)i = '很抱歉，我们的网站有些问题，您可以稍候再试一次';
        if(stat == 2 || stat == 4 || stat == 8) {
            d.style.backgroundColor = '#FF6600';
            d.style.color = '#FFF';
        } else {
            //动画效果
            poofCount = 0;
            poofTimer = setInterval("animatePoof('car_eff')", 75);

            $('promo').show();
            d.style.backgroundColor = '#339933';
            d.style.color = '#FFFFFF';
        }
    }
    catch(e) {
        i = '很抱歉，我们的网站有些问题，您可以稍候再试一次';
        d.style.backgroundColor = '#FF6600';
        d.style.color = '#FFF';
    }
    if(stat == 1) {
		$('cartext').update('您的购物车中<br />共有 <span id="caramount">' + c + '<\/span> 件战利品');
		$('caramount').innerHTML = c;
		$('icon_shopcar').addClassName('icon_shopcar_full');
    }
    if(showmsg == true || stat == 8) {
        try {
            d.innerHTML = '&nbsp;&nbsp;&nbsp;' + i + '...';
        }
        catch(e) {
            d.innerHTML = '&nbsp;&nbsp;&nbsp;' + i.replace(/<a.+$/,'');
        }
        d.show();
    } else {
        setCookie("tips_drop", 1, expires_date, "/");
        d.hide();
    }

    new Effect.Round('div', 'updatemsg');
    if(timer) window.clearTimeout(timer);
    timer = window.setTimeout("$('updatemsg').hide()", 20000);
}

function _dg(m, ele) {
    if (typeof ele == 'undefined') ele = $('itemlist_table') || $('itemlist');
    $A(ele.getElementsByTagName('a')).each(function(node) {
        node.onclick = function(){return m;}
    });
}

function __de(ele) {
    window.setTimeout(function(){_dg(true,ele.element)}, 1000);
}

function __dg(ele) {
    return _dg(false, ele.element);
}

